//
//  DetailAddressViewController.m
//  MemberProject
//
//  Created by vincent luo on 15/4/30.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "DetailAddressViewController.h"
#import "CMPopTipView.h"
//#import "BNCoreServices.h"
#import "CustomPaopaoView.h"

@interface DetailAddressViewController ()

@end

@implementation DetailAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _mapView.delegate = self;
    _mapView.zoomLevel =14;
    [self setGeocoder];
    [self addAnnotation];
    
}
-(void)setGeocoder
{
//    CLGeocoder
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.mapView viewWillAppear];
    _mapView.delegate = self;
    _geocodesearch.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    _geocodesearch.delegate = nil;
}
-(void)dealloc
{
    _geocodesearch = nil;
    _mapView = nil;
}
-(void)addAnnotation
{
    CLLocationCoordinate2D coor;
    coor.latitude = _targetCoordinate.latitude;
    coor.longitude = _targetCoordinate.longitude;
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = coor;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    
    
//    annotation.coordinate = coor;
//    annotation.title = @"这里";
//    [_mapView addAnnotation:annotation];
    
//    [_mapView setRegion:BMKCoordinateRegionMakeWithDistance(coor, 0.1, 0.1)];
    
    
}
//- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
//{
//    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
//        
//        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
//        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 20)];
//        [btn setTitle:@"导航" forState:0];
//        [newAnnotationView setSelected:YES animated:YES];
//        [btn setTitleColor:[UIColor greenColor] forState:0];
//        newAnnotationView.rightCalloutAccessoryView =btn;
//        newAnnotationView.animatesDrop = NO;// 设置该标注点动画显示
//        newAnnotationView.image=[self getImageFromView:viewForImage];
//        return newAnnotationView;
//    }
//    return nil;
//}
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
//在这个委托中实现如下代
    BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
    
    newAnnotationView.paopaoView.frame = CGRectMake(0, 0, 200, 100);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [btn addTarget:self action:@selector(startNavi) forControlEvents:64];
    newAnnotationView.rightCalloutAccessoryView = btn;
    
//    CustomPaopaoView *pView = [[CustomPaopaoView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
//    pView.backgroundColor = [UIColor redColor];
//    newAnnotationView.paopaoView = [[BMKActionPaopaoView alloc]initWithCustomView:pView] ;
    
    newAnnotationView.animatesDrop = YES;
    [newAnnotationView setSelected:YES animated:YES];
    return newAnnotationView;
}

#pragma mark 导航
////发起导航
//- (void)startNavi
//{
//    //节点数组
//    NSMutableArray *nodesArray = [[NSMutableArray alloc]    initWithCapacity:2];
//    
//    //起点
//    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
//    startNode.pos = [[BNPosition alloc] init];
//    startNode.pos.x = _targetCoordinate.longitude;
//    startNode.pos.y = _targetCoordinate.latitude;
//    startNode.pos.eType = BNCoordinate_BaiduMapSDK;
//    [nodesArray addObject:startNode];
//    
//    //终点
//    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
//    endNode.pos = [[BNPosition alloc] init];
//    endNode.pos.x = 114.077075;
//    endNode.pos.y = 22.543634;
//    endNode.pos.eType = BNCoordinate_BaiduMapSDK;
//    [nodesArray addObject:endNode];
//    //发起路径规划
//    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
//}
////算路成功回调
//-(void)routePlanDidFinished:(NSDictionary *)userInfo
//{
//    NSLog(@"算路成功");
//    
//    //路径规划成功，开始导航
//    [BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:self isNeedLandscape:YES];
//}
//-(void)onExitNaviUI:(NSDictionary*)extraInfo
//{
//    NSLog(@"退出导航页面");
//}
//退出导航声明页面回调
//- (void)onExitDeclarationUI:(NSDictionary*)extraInfo
//{
//    NSLog(@"退出导航声明页面");
//}

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == 0) {
        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
        item.coordinate = result.location;
        item.title = result.address;
        [_mapView addAnnotation:item];
        _mapView.centerCoordinate = result.location;

    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
